package com.example.boxugu.myapplication.Fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxugu.myapplication.Moder.UserInfoModer;
import com.example.boxugu.myapplication.Moder.VediodetailModer;
import com.example.boxugu.myapplication.R;
import com.example.boxugu.myapplication.activity.ChangePasswdActivity;
import com.example.boxugu.myapplication.activity.MainActivity;
import com.example.boxugu.myapplication.activity.RegisterActivity;
import com.example.boxugu.myapplication.activity.UserInfoActivity;
import com.example.boxugu.myapplication.activity.loginActivity;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import static android.content.Context.MODE_PRIVATE;


public class MyFragment extends Fragment {
   TextView mlogin,setting,exit;
   String token;
   ImageView imageView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_my, container, false);
        mlogin=view.findViewById(R.id.text_lgoin);
        setting=view.findViewById(R.id.setting_text);
        imageView=view.findViewById(R.id.img2);
        exit=view.findViewById(R.id.exit_text);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (token.length()>0){
                    Intent i=new Intent(getActivity(),UserInfoActivity.class);
                    startActivity(i);
                }else {
                    Intent i=new Intent(getActivity(),loginActivity.class);
                    startActivity(i);
                }
            }
        });
        final SharedPreferences sharedPreferences=getActivity().getSharedPreferences("boxugu", MODE_PRIVATE);
        token=sharedPreferences.getString("token","");
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (token.length()>0){
                    Toast.makeText(getActivity(),"退出成功",Toast.LENGTH_LONG).show();
                    SharedPreferences sp=getActivity().getSharedPreferences("boxugu", MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.clear();
                    editor.commit();
                    Intent i=new Intent(getActivity(),MainActivity.class);
                    startActivity(i);
                }else {
                    Toast.makeText(getActivity(),"请登录用户",Toast.LENGTH_LONG).show();
                    Intent i=new Intent(getActivity(),loginActivity.class);
                    startActivity(i);
                }
            }
        });

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (token.length()>0){
                    Intent i=new Intent(getActivity(),UserInfoActivity.class);
                    startActivity(i);
                }else {
                    Intent i=new Intent(getActivity(),loginActivity.class);
                    startActivity(i);
                }

            }
        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (token.length()>0){
                Intent i=new Intent(getActivity(), ChangePasswdActivity.class);
                startActivity(i);
            }else {
                    Toast.makeText(getActivity(),"请登录用户",Toast.LENGTH_LONG).show();
                }
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        if (token.length()>0){
            RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/getuserinfo");
            params.addBodyParameter("token",token);
            x.http().post(params, new Callback.CacheCallback<String>() {
                @SuppressLint("ResourceType")
                @Override
                public void onSuccess(String result) {
                    Gson gson = new Gson();
                    UserInfoModer userInfoModer = gson.fromJson(result, UserInfoModer.class);
                    if (userInfoModer!=null&&userInfoModer.data!=null){
                        mlogin.setText(userInfoModer.data.username);
                    }


                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {


                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }

                @Override
                public boolean onCache(String result) {
                    return false;
                }
            });
        }
        super.onResume();

    }


}
